package com.jy.event.service.impl;

import com.jy.event.dao.OrderDao;
import com.jy.event.dao.PaymentDao;
import com.jy.event.dao.StoreDao;
import com.jy.event.domain.Order;
import com.jy.event.domain.OrderStatus;
import com.jy.event.domain.Payment;
import com.jy.event.domain.Store;
import com.jy.event.pay.request.PayRequest;
import com.jy.event.pay.result.PayResult;
import com.jy.event.service.OrderService;
import com.jy.event.util.ObjectToMap;
import com.jy.event.util.XmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lihu on 2017/12/29.
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    private static final String ALI_QR_CODE = "https://pay.swiftpass.cn/pay/gateway";

    private final static String URL = "APIGATEWAYURL/v1/system/notice";

    private final static String ORDER_HANDLE_URL = "APIGATEWAYURL//v1/synch/PAYMENT_ID/order";


    @Value("${api.gateway.url}")
    private String apiGatewayUrl;

    @Autowired
    PaymentDao paymentDao;

    @Autowired
    StoreDao storeDao;


    @Autowired
    OrderDao orderDao;

    @Autowired
    RestTemplate restTemplate;


    @Override
    public void chekeOrder1(Payment payment) {
        Order order = orderDao.findOne(payment.getOrderId());
        if (order.getStatus() == OrderStatus.UNFINISHED &&
                StringUtils.equals(order.getPayId(), payment.getId())) {
            log.info("send ");
            Map<String, Object> map = new HashMap<>();
            map.put("orderId", order.getId());
            restTemplate.postForEntity(URL.replace("APIGATEWAYURL", apiGatewayUrl), map, String.class);
        }


    }

    @Override
    @Transactional
    public void chekeOrder(Payment payment) {
        Store store = storeDao.findOne(payment.getStoreId());
        PayResult payResult = query(store, payment.getId());
        log.info("payResult result:{}", payResult);
        if (payResult != null && payResult.success()) {
            restTemplate.postForEntity(ORDER_HANDLE_URL.
                    replace("APIGATEWAYURL", apiGatewayUrl).replace("PAYMENT_ID", payment.getId()), "", String.class);
        }


    }


    //支付查询
    private PayResult query(Store store, String payId) {
        try {
            //初始化请求实体
            PayRequest payRequest = new PayRequest("unified.trade.query", store.getMchId(), payId);
            payRequest.setUpSign(ObjectToMap.objectToMap(payRequest), store.getKey());
            log.info("query PayRequest:{}", payRequest);
            String xml = restTemplate.postForEntity(ALI_QR_CODE, payRequest.toXML(), String.class).getBody();
            PayResult queryResult = XmlUtils.fromXML(xml, PayResult.class);
            return queryResult;
        } catch (Exception ex) {
            log.error("order is error:{}", ex);
            return null;
        }

    }
}
